library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3     ✓ purrr   0.3.4
## ✓ tibble  3.1.0     ✓ dplyr   1.0.5
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
dssi <- read_csv("dssi_2020.csv")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   `Country Name` = col_character(),
##   `Country Code` = col_character(),
##   `Counterpart-Area Name` = col_character(),
##   `Counterpart-Area Code` = col_character(),
##   `Series Name` = col_character(),
##   `Series Code` = col_character(),
##   `2000` = col_character(),
##   `2008` = col_character(),
##   `2019` = col_character(),
##   `2020` = col_character()
## )

Data source: https://databank.worldbank.org/source/international-debt-statistics:-dssi

Georgetown report: http://gppreview.com/2021/02/10/international-debt-exploring-new-data-world-bank/

“In 2020, the World Bank – reacting to global financial shocks from COVID-19 and emphasizing debt transparency – for the first time released individual creditor countries within “International Debt Statistics.” The interactive map below visualizes these newly released creditor country breakdowns. Previously, such data was only available at a generalized “creditor region” level. Such a release provides a tremendous level of granularity, which economic and public policy analysts can use to examine relationships between the 120 low and middle-income countries of the world."

World Bank report: https://openknowledge.worldbank.org/bitstream/handle/10986/34588/9781464816109.pdf

About the DSSI:
https://www.worldbank.org/en/topic/debt/brief/covid-19-debt-service-suspension-initiative

In all, 73 countries are eligible for a temporary suspension of debt-service payments owed to their official bilateral creditors. The G20 has also called on private creditors to participate in the initiative on comparable terms. The suspension period, originally set to end on December 31, 2020, has been extended through June 2021.

Net inflows from the World Bank (International Bank for Reconstruction and Development [IBRD] and International Development Association [IDA])

“Estimates are derived from monthly IDS projections based on end-2019 external public and publicly guaranteed debt outstanding and disbursed.”

2020 data only available for debt service and principal repayments…

Cleaning the data:

dssi <- dssi %>% select(-`Series Code`) %>% gather("Year", "Value", 6:9)

dssi <- dssi %>% anti_join(dssi[c(29785, 29786, 29787, 29788, 29789, 59574, 59575, 59576, 59577, 59578, 89363, 89364, 89365, 89366, 89367, 119152, 119153, 119154, 119155, 119156),])
## Joining, by = c("Country Name", "Country Code", "Counterpart-Area Name", "Counterpart-Area Code", "Series Name", "Year", "Value")
dssi <- dssi %>% spread(`Series Name`, `Value`)

colnames(dssi) <- c("Debtor", "Debtor_Code", "Creditor", "Creditor_Code", "Year", "Debt_Forgiveness", "Debt_Stocks")

dssi$Debt_Forgiveness <- as.numeric(dssi$Debt_Forgiveness)
## Warning: NAs introduced by coercion
dssi$Debt_Stocks <- as.numeric(dssi$Debt_Stocks)
## Warning: NAs introduced by coercion
dssi$Year <- as.factor(dssi$Year)

dssi[is.na(dssi)] <- 0

dssi$Debtor <- stringr::str_trim(dssi$Debtor, side = "both")
dssi$Creditor <- stringr::str_trim(dssi$Creditor, side = "both")

dssi <- dssi %>% mutate(Debtor_Region = case_when(
  Debtor %in% c("Angola", "Benin", "Burkina Faso", "Burundi", "Cabo Verde", "Cameroon", "Central African Republic", "Chad", "Comoros", "Congo, Dem. Rep.", "Congo, Rep.", "Cote d'Ivoire", "Djibouti", "Ethiopia", "Gambia, The", "Ghana", "Guinea", "Guinea-Bissau", "Kenya", "Lesotho", "Liberia", "Madagascar", "Malawi", "Mali", "Mauritania", "Mozambique", "Niger", "Nigeria", "Rwanda", "Sao Tome and Principe", "Senegal", "Sierra Leone", "Somalia", "Tanzania", "Togo", "Uganda", "Zambia") ~ "Africa",
  Debtor %in% c("Bangladesh", "Bhutan", "Cambodia", "Kyrgyz Republic", "Lao PDR", "Maldives", "Mongolia", "Myanmar", "Nepal", "Pakistan", "Tajikistan", "Timor-Leste", "Uzbekistan") ~ "Asia",
  Debtor %in% c("Afghanistan", "Yemen, Rep.") ~ "Middle East",
  Debtor %in% c("Guyana", "Haiti", "Honduras", "Nicaragua") ~ "South & Central America",
  Debtor %in% c("Dominica", "Grenada", "St. Lucia", "St. Vincent and the Grenadines") ~ "The Caribbeans",
  Debtor %in% c("Fiji", "Papua New Guinea", "Samoa", "Solomon Islands", "Tonga", "Vanuatu") ~ "Pacific Islands",
  Debtor %in% c("Kosovo", "Moldova") ~ "Europe"
))

Debtor countries:

dssi$Debtor %>% unique()
##  [1] "Afghanistan"                    "Angola"                        
##  [3] "Bangladesh"                     "Benin"                         
##  [5] "Bhutan"                         "Burkina Faso"                  
##  [7] "Burundi"                        "Cabo Verde"                    
##  [9] "Cambodia"                       "Cameroon"                      
## [11] "Central African Republic"       "Chad"                          
## [13] "Comoros"                        "Congo, Dem. Rep."              
## [15] "Congo, Rep."                    "Cote d'Ivoire"                 
## [17] "Djibouti"                       "Dominica"                      
## [19] "Ethiopia"                       "Fiji"                          
## [21] "Gambia, The"                    "Ghana"                         
## [23] "Grenada"                        "Guinea"                        
## [25] "Guinea-Bissau"                  "Guyana"                        
## [27] "Haiti"                          "Honduras"                      
## [29] "Kenya"                          "Kosovo"                        
## [31] "Kyrgyz Republic"                "Lao PDR"                       
## [33] "Lesotho"                        "Liberia"                       
## [35] "Madagascar"                     "Malawi"                        
## [37] "Maldives"                       "Mali"                          
## [39] "Mauritania"                     "Moldova"                       
## [41] "Mongolia"                       "Mozambique"                    
## [43] "Myanmar"                        "Nepal"                         
## [45] "Nicaragua"                      "Niger"                         
## [47] "Nigeria"                        "Pakistan"                      
## [49] "Papua New Guinea"               "Rwanda"                        
## [51] "Samoa"                          "Sao Tome and Principe"         
## [53] "Senegal"                        "Sierra Leone"                  
## [55] "Solomon Islands"                "Somalia"                       
## [57] "St. Lucia"                      "St. Vincent and the Grenadines"
## [59] "Tajikistan"                     "Tanzania"                      
## [61] "Timor-Leste"                    "Togo"                          
## [63] "Tonga"                          "Uganda"                        
## [65] "Uzbekistan"                     "Vanuatu"                       
## [67] "Yemen, Rep."                    "Zambia"

68 in total.

dssi %>% select(Debtor, Debtor_Region) %>% unique() %>% group_by(Debtor_Region) %>% count() %>% arrange(desc(n))
## # A tibble: 7 x 2
## # Groups:   Debtor_Region [7]
##   Debtor_Region               n
##   <chr>                   <int>
## 1 Africa                     37
## 2 Asia                       13
## 3 Pacific Islands             6
## 4 South & Central America     4
## 5 The Caribbeans              4
## 6 Europe                      2
## 7 Middle East                 2

Most debtor countries are from Africa, followed by Asia.

Creditors:

dssi$Creditor %>% unique()
##   [1] "Afghanistan"                    "African Dev. Bank"             
##   [3] "Albania"                        "Algeria"                       
##   [5] "Andorra"                        "Angola"                        
##   [7] "Anguilla"                       "Antigua"                       
##   [9] "Argentina"                      "Armenia"                       
##  [11] "Aruba"                          "Asian Dev. Bank"               
##  [13] "Australia"                      "Austria"                       
##  [15] "Azerbaijan"                     "Bahamas"                       
##  [17] "Bahrain"                        "Bangladesh"                    
##  [19] "Barbados"                       "Belarus"                       
##  [21] "Belgium"                        "Belize"                        
##  [23] "Benin"                          "Bermuda"                       
##  [25] "Bhutan"                         "Bolivia"                       
##  [27] "Bondholders"                    "Bosnia-Herzegovina"            
##  [29] "Botswana"                       "Brazil"                        
##  [31] "Brunei"                         "Bulgaria"                      
##  [33] "Burkina Faso"                   "Burundi"                       
##  [35] "Cabo Verde"                     "Cambodia"                      
##  [37] "Cameroon"                       "Canada"                        
##  [39] "Cayman Islands"                 "Central African Republic"      
##  [41] "Chad"                           "Chile"                         
##  [43] "China"                          "Colombia"                      
##  [45] "Comoros"                        "Congo, Dem. Rep."              
##  [47] "Congo, Rep."                    "Costa Rica"                    
##  [49] "Cote D`Ivoire, Republic Of"     "Croatia"                       
##  [51] "Cuba"                           "Cyprus"                        
##  [53] "Czech Republic"                 "Czechoslovakia"                
##  [55] "Denmark"                        "Djibouti"                      
##  [57] "Dominica"                       "Dominican Republic"            
##  [59] "Ecuador"                        "Egypt"                         
##  [61] "El Salvador"                    "Equatorial Guinea"             
##  [63] "Eritrea"                        "Estonia"                       
##  [65] "Eswatini"                       "Ethiopia"                      
##  [67] "Fiji"                           "Finland"                       
##  [69] "France"                         "French Polynesia"              
##  [71] "Gabon"                          "Gambia, The"                   
##  [73] "Georgia"                        "German Dem. Rep."              
##  [75] "Germany, Fed.Rep. Of"           "Ghana"                         
##  [77] "Gibraltar"                      "Greece"                        
##  [79] "Grenada"                        "Guadeloupe"                    
##  [81] "Guam"                           "Guatemala"                     
##  [83] "Guinea"                         "Guinea-Bissau"                 
##  [85] "Guyana"                         "Haiti"                         
##  [87] "Honduras"                       "Hong Kong"                     
##  [89] "Hungary"                        "Iceland"                       
##  [91] "India"                          "Indonesia"                     
##  [93] "Inter-American Dev. Bank"       "International Monetary Fund"   
##  [95] "Iran, Islamic Republic Of"      "Iraq"                          
##  [97] "Ireland"                        "Israel"                        
##  [99] "Italy"                          "Jamaica"                       
## [101] "Japan"                          "Jordan"                        
## [103] "Kazakhstan"                     "Kenya"                         
## [105] "Kiribati"                       "Korea, D.P.R. Of"              
## [107] "Korea, Republic Of"             "Kosovo"                        
## [109] "Kuwait"                         "Kyrgyz Republic"               
## [111] "Lao Peoples Democratic Repub."  "Latvia"                        
## [113] "Lebanon"                        "Lesotho"                       
## [115] "Liberia"                        "Libya"                         
## [117] "Lithuania"                      "Luxembourg"                    
## [119] "Macao"                          "Madagascar"                    
## [121] "Malawi"                         "Malaysia"                      
## [123] "Maldives"                       "Mali"                          
## [125] "Malta"                          "Mauritania"                    
## [127] "Mauritius"                      "Mexico"                        
## [129] "Micronesia Fed Sts"             "Moldova"                       
## [131] "Monaco"                         "Mongolia"                      
## [133] "Montenegro"                     "Morocco"                       
## [135] "Mozambique"                     "Multiple Lenders"              
## [137] "Myanmar"                        "Namibia"                       
## [139] "Nepal"                          "Neth. Antilles"                
## [141] "Netherlands"                    "New Caledonia (Fr.)"           
## [143] "New Zealand"                    "Nicaragua"                     
## [145] "Niger"                          "Nigeria"                       
## [147] "North Macedonia"                "Norway"                        
## [149] "Oman"                           "Other Bilateral"               
## [151] "Other Multilaterals"            "Other Multiple Lenders"        
## [153] "Pacific Is. (Us)"               "Pakistan"                      
## [155] "Panama"                         "Papua New Guinea"              
## [157] "Paraguay"                       "Peru"                          
## [159] "Philippines"                    "Poland"                        
## [161] "Portugal"                       "Puerto Rico"                   
## [163] "Qatar"                          "Reunion"                       
## [165] "Romania"                        "Russian Federation"            
## [167] "Rwanda"                         "Samoa"                         
## [169] "Sao Tome & Principe"            "Saudi Arabia"                  
## [171] "Senegal"                        "Serbia"                        
## [173] "Seychelles"                     "Sierra Leone"                  
## [175] "Singapore"                      "Slovak Republic"               
## [177] "Slovenia"                       "Solomon Islands"               
## [179] "Somalia"                        "South Africa"                  
## [181] "Spain"                          "Sri Lanka"                     
## [183] "St. Kitts And Nevis"            "St. Lucia"                     
## [185] "St. Vincent & The Grenadines"   "Sudan"                         
## [187] "Surinam"                        "Sweden"                        
## [189] "Switzerland"                    "Syrian Arab Republic"          
## [191] "Tajikistan"                     "Tanzania"                      
## [193] "Thailand"                       "Timor-Leste"                   
## [195] "Togo"                           "Tonga"                         
## [197] "Trinidad & Tobago"              "Tunisia"                       
## [199] "Turkey"                         "Turkmenistan"                  
## [201] "Uganda"                         "Ukraine"                       
## [203] "United Arab Emirates"           "United Kingdom"                
## [205] "United States"                  "Uruguay"                       
## [207] "USSR"                           "Uzbekistan"                    
## [209] "Vanuatu"                        "Venezuela, Republic Bolivarian"
## [211] "Vietnam"                        "Virgin Is.(US)"                
## [213] "World"                          "World Bank-IBRD"               
## [215] "World Bank-IDA"                 "Yemen, Republic Of"            
## [217] "Yugoslavia"                     "Zambia"                        
## [219] "Zimbabwe"

Includes both countries and multilateral organizations.

Overall debt levels

dssi %>% filter(Creditor == "World") %>% filter(Year %in% c(2000, 2019)) %>% group_by(Year) %>% summarize(global_debt_bil = sum(Debt_Stocks)/10^9)
## # A tibble: 2 x 2
##   Year  global_debt_bil
##   <fct>           <dbl>
## 1 2000             262.
## 2 2019             744.

Total global debt increased by more than 180% from US$261 billion in 2000 to US\(744 billion in 2019. This is denominated in current US\) (2021) and hence accounts for inflationary effects.

Debt levels

ggplot(dssi %>% filter(Year != 2020)) + geom_boxplot(aes(as.factor(Year), Debt_Stocks/10^9)) + labs(x = "Year", y = "External debt level (billions)", title = "External debt stocks over time")

debt_2000 <- dssi %>% filter(Year == 2000)
debt_2019 <- dssi %>% filter(Year == 2019)
ggplot(debt_2000 %>% filter(Creditor == "World")) + geom_dotplot(aes(Debt_Stocks/10^9), binwidth = 1)

ggplot(debt_2019 %>% filter(Creditor == "World")) + geom_dotplot(aes(Debt_Stocks/10^9), binwidth = 1)

debt_2000 %>% filter(Creditor == "World")  %>% mutate(Debt_Stocks_Bil = Debt_Stocks/10^9) %>% select(Debtor, Creditor, Debt_Stocks_Bil) %>% arrange(desc(Debt_Stocks_Bil))
## # A tibble: 68 x 3
##    Debtor           Creditor Debt_Stocks_Bil
##    <chr>            <chr>              <dbl>
##  1 Nigeria          World              33.5 
##  2 Pakistan         World              33.1 
##  3 Bangladesh       World              15.6 
##  4 Cote d'Ivoire    World              12.1 
##  5 Congo, Dem. Rep. World              11.8 
##  6 Cameroon         World              10.6 
##  7 Angola           World               9.76
##  8 Tanzania         World               7.19
##  9 Nicaragua        World               6.82
## 10 Ghana            World               6.74
## # … with 58 more rows
debt_2019 %>% filter(Creditor == "World")  %>% mutate(Debt_Stocks_Bil = Debt_Stocks/10^9) %>% select(Debtor, Creditor, Debt_Stocks_Bil) %>% arrange(desc(Debt_Stocks_Bil))
## # A tibble: 68 x 3
##    Debtor     Creditor Debt_Stocks_Bil
##    <chr>      <chr>              <dbl>
##  1 Pakistan   World              101. 
##  2 Bangladesh World               57.1
##  3 Nigeria    World               54.8
##  4 Angola     World               52.0
##  5 Kenya      World               34.2
##  6 Mongolia   World               31.4
##  7 Ethiopia   World               28.3
##  8 Zambia     World               27.3
##  9 Ghana      World               27.0
## 10 Uzbekistan World               21.7
## # … with 58 more rows

2000: Nigeria ($33.5 bil), Pakistan ($33.9 bil), Bangladesh ($15.6 bil), Cote d’Ivoire ($12.1 bil), Angola was ranked 7th at $9.8 bil.

2019: Pakistan $100 billion followed by Bangladesh ($57 bil), Nigeria ($54 bil), Angola ($51 bil).

debt_2000 %>% group_by(Debtor_Region) %>% filter(Creditor == "World") %>% summarize(total_debt_bil = sum(Debt_Stocks)/10^9, avg_debt_bil = mean(Debt_Stocks)/10^9) %>% arrange(desc(total_debt_bil))
## # A tibble: 7 x 3
##   Debtor_Region           total_debt_bil avg_debt_bil
##   <chr>                            <dbl>        <dbl>
## 1 Africa                         164.           4.43 
## 2 Asia                            72.1          5.55 
## 3 South & Central America         15.0          3.75 
## 4 Middle East                      5.15         2.58 
## 5 Pacific Islands                  2.93         0.488
## 6 Europe                           1.91         0.957
## 7 The Caribbeans                   0.800        0.200
debt_2019 %>% group_by(Debtor_Region) %>% filter(Creditor == "World") %>% summarize(total_debt_bil = sum(Debt_Stocks)/10^9, avg_debt_bil = mean(Debt_Stocks)/10^9) %>% arrange(desc(total_debt_bil))
## # A tibble: 7 x 3
##   Debtor_Region           total_debt_bil avg_debt_bil
##   <chr>                            <dbl>        <dbl>
## 1 Africa                          395.         10.7  
## 2 Asia                            281.         21.6  
## 3 South & Central America          25.3         6.32 
## 4 Pacific Islands                  21.1         3.52 
## 5 Europe                            9.96        4.98 
## 6 Middle East                       9.72        4.86 
## 7 The Caribbeans                    1.83        0.457

Creditors

2000:

debt_2000 %>% group_by(Creditor) %>% summarize(total_lending_bil = sum(Debt_Stocks)/10^9) %>% arrange(desc(total_lending_bil))
## # A tibble: 219 x 2
##    Creditor                    total_lending_bil
##    <chr>                                   <dbl>
##  1 World                                   262. 
##  2 World Bank-IDA                           49.0
##  3 Multiple Lenders                         26.8
##  4 Other Multiple Lenders                   21.7
##  5 Japan                                    20.5
##  6 Asian Dev. Bank                          14.0
##  7 France                                   13.9
##  8 International Monetary Fund              12.6
##  9 Russian Federation                       11.4
## 10 African Dev. Bank                        10.9
## # … with 209 more rows

Top country creditors: Japan, France, Russian Federation, US, Germany, Italy, UK, China

debt_2000 %>% group_by(Creditor) %>% summarize(total_lending_bil = sum(Debt_Stocks)/10^9) %>% ggplot() + geom_dotplot(aes(total_lending_bil))
## `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.

2019:

debt_2019 %>% group_by(Creditor) %>% summarize(total_lending_bil = sum(Debt_Stocks)/10^9) %>% arrange(desc(total_lending_bil))
## # A tibble: 219 x 2
##    Creditor                    total_lending_bil
##    <chr>                                   <dbl>
##  1 World                                   744. 
##  2 Other Multiple Lenders                  203. 
##  3 World Bank-IDA                          111. 
##  4 China                                   109. 
##  5 Bondholders                              76.3
##  6 Asian Dev. Bank                          36.2
##  7 International Monetary Fund              32.4
##  8 Other Multilaterals                      31.2
##  9 Japan                                    24.1
## 10 African Dev. Bank                        23.1
## # … with 209 more rows

19 years later, China is now the top creditor country. India and Saudi Arabia have also emerged, ranking above the US and Germany.

Pie Charts~

library(highcharter) 
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
cols <- RColorBrewer::brewer.pal(8, "Dark2")

top_creditors_2000 <- debt_2000 %>% filter(!Creditor %in% c("World", "Multiple Lenders", "Other Multiple Lenders", "Other Multilaterals", "Bondholders", "Other Bilateral")) %>% group_by(Creditor) %>% summarize(total_debt_bil = sum(Debt_Stocks)/10^9) %>% mutate(pct_global_debt = total_debt_bil/261.6634) %>% arrange(desc(total_debt_bil)) %>% head(n = 20)

top_creditors_2019 <- debt_2019 %>% filter(!Creditor %in% c("World", "Multiple Lenders", "Other Multiple Lenders", "Other Multilaterals", "Bondholders", "Other Bilateral")) %>% group_by(Creditor) %>% summarize(total_debt_bil = sum(Debt_Stocks)/10^9) %>% mutate(pct_global_debt = total_debt_bil/744.4824) %>% arrange(desc(total_debt_bil)) %>% head(n = 20) 

top_creditors_2000 %>% hchart("pie", hcaes(x = Creditor, y = total_debt_bil), name = "Total Lending (billions of US$)") %>% hc_title(text = "Top creditors in 2000") %>% hc_subtitle(text = "subtitle") %>% hc_caption(text = "Data: The World Bank (2021)") %>% hc_colors(cols)
top_creditors_2019 %>% hchart("pie", hcaes(x = Creditor, y = total_debt_bil), name = "Total Lending (billions of US$)") %>% hc_title(text = "Top creditors in 2019") %>% hc_subtitle(text = "subtitle") %>% hc_caption(text = "Data: The World Bank (2021)") %>% hc_colors(cols)
## Is there a way to highlight China?
# or maybe I should choose top creditors based on total across the years
top_creditors <- top_creditors_2000 %>% pull(Creditor) %>% append(top_creditors_2019 %>% pull(Creditor)) %>% unique()

top_creditor_stats <- dssi %>% filter(Creditor %in% top_creditors) %>% filter(Year %in% c(2000, 2019)) %>% group_by(Creditor, Year) %>% summarize(total_lending_bil = sum(Debt_Stocks)/10^9, total_forgiveness_bil = sum(Debt_Forgiveness)/10^9)
## `summarise()` has grouped output by 'Creditor'. You can override using the `.groups` argument.
top_creditor_stats$pct_global_debt <- ifelse(top_creditor_stats$Year == 2000, top_creditor_stats$total_lending_bil/261.6634, top_creditor_stats$total_lending_bil/744.4824)

ggplot(top_creditor_stats) + geom_col(aes(x = reorder(Creditor, total_lending_bil), y = total_lending_bil, fill = as.factor(Year)), position = "dodge") + coord_flip()

ggplot(top_creditor_stats) + geom_col(aes(x = reorder(Creditor, pct_global_debt), y = pct_global_debt, fill = as.factor(Year)), position = "dodge") + coord_flip() + theme_minimal()

## How do I show 2000 before 2019?

As a percentage: China has increased dramatically. International banks including the World Bank, ADB, IMF, African Development Bank have all decreased, as with most other countries. Stark decrease in France, Russia, US, Germany, Japan.

Selected Creditors over time (those with drastic changes, top creditors)

Data cleaning:

selected_creditors <- read_csv("selected_creditors.csv")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   .default = col_character()
## )
## ℹ Use `spec()` for the full column specifications.
selected_creditors <- selected_creditors %>% select(-`Series Code`)
colnames(selected_creditors) <- c("Debtor", "Debtor_Code", "Creditor", "Creditor_Code", "Series", 2000:2019)

selected_creditors <- selected_creditors %>% gather("year", "value", 6:25)

selected_creditors[which(selected_creditors$Series == "External debt stocks, total (DOD, current US$)"), "Series"] <- "Debt_Stocks"
selected_creditors[which(selected_creditors$Series == "Debt forgiveness or reduction (current US$)"), "Series"] <- "Debt_Forgiveness"

selected_creditors <- selected_creditors %>% filter(!is.na(Series))

selected_creditors <- spread(selected_creditors, Series, value)


selected_creditors$Debt_Forgiveness <- as.numeric(selected_creditors$Debt_Forgiveness)
## Warning: NAs introduced by coercion
selected_creditors$Debt_Stocks <- as.numeric(selected_creditors$Debt_Stocks)
## Warning: NAs introduced by coercion
selected_creditors$year <- as.numeric(selected_creditors$year)

selected_creditors[is.na(selected_creditors)] <- 0

selected_creditors$Debtor <- stringr::str_trim(selected_creditors$Debtor, side = "both")
selected_creditors$Creditor <- stringr::str_trim(selected_creditors$Creditor, side = "both")

selected_creditors <- selected_creditors %>% mutate(Debtor_Region = case_when(
  Debtor %in% c("Angola", "Benin", "Burkina Faso", "Burundi", "Cabo Verde", "Cameroon", "Central African Republic", "Chad", "Comoros", "Congo, Dem. Rep.", "Congo, Rep.", "Cote d'Ivoire", "Djibouti", "Ethiopia", "Gambia, The", "Ghana", "Guinea", "Guinea-Bissau", "Kenya", "Lesotho", "Liberia", "Madagascar", "Malawi", "Mali", "Mauritania", "Mozambique", "Niger", "Nigeria", "Rwanda", "Sao Tome and Principe", "Senegal", "Sierra Leone", "Somalia", "Tanzania", "Togo", "Uganda", "Zambia") ~ "Africa",
  Debtor %in% c("Bangladesh", "Bhutan", "Cambodia", "Kyrgyz Republic", "Lao PDR", "Maldives", "Mongolia", "Myanmar", "Nepal", "Pakistan", "Tajikistan", "Timor-Leste", "Uzbekistan") ~ "Asia",
  Debtor %in% c("Afghanistan", "Yemen, Rep.") ~ "Middle East",
  Debtor %in% c("Guyana", "Haiti", "Honduras", "Nicaragua") ~ "South & Central America",
  Debtor %in% c("Dominica", "Grenada", "St. Lucia", "St. Vincent and the Grenadines") ~ "The Caribbeans",
  Debtor %in% c("Fiji", "Papua New Guinea", "Samoa", "Solomon Islands", "Tonga", "Vanuatu") ~ "Pacific Islands",
  Debtor %in% c("Kosovo", "Moldova") ~ "Europe"
))
creditors_years <- selected_creditors %>% group_by(Creditor, year) %>% summarize(total_lending_bil = sum(Debt_Stocks)/10^9)
## `summarise()` has grouped output by 'Creditor'. You can override using the `.groups` argument.
ggplot(creditors_years) + geom_line(aes(x = year, y = total_lending_bil, color = Creditor)) + theme_minimal()

ggplotly(ggplot(creditors_years) + geom_line(aes(x = year, y = total_lending_bil, color = Creditor)) + theme_minimal()) 
ggplot(creditors_years %>% filter(Creditor %in% c("China", "Japan", "United States", "United Kingdom", "France", "Saudi Arabia", "India"))) + geom_line(aes(x = year, y = total_lending_bil, color = Creditor)) + theme_minimal()

ggplotly(ggplot(creditors_years %>% filter(Creditor %in% c("China", "Japan", "United States", "United Kingdom", "France", "Saudi Arabia", "India"))) + geom_line(aes(x = year, y = total_lending_bil, color = Creditor)) + theme_minimal())
## Very clear jump from China :0

By region

ggplot(selected_creditors %>% filter(Creditor %in% c("China", "Japan", "United States", "United Kingdom", "France", "Saudi Arabia", "India")) %>% filter(Debtor_Region == "Asia")) + geom_smooth(aes(x = year, y = Debt_Stocks, color = Creditor)) + theme_minimal()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(selected_creditors %>% filter(Creditor %in% c("China", "Japan", "United States", "United Kingdom", "France", "Saudi Arabia", "India")) %>% filter(Debtor_Region == "Africa")) + geom_smooth(aes(x = year, y = Debt_Stocks, color = Creditor)) + theme_minimal()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Next steps: Explore by region, network visualization